package cn.wgx.module.sys.mapper;

import cn.wgx.common.base.IBaseMapper;
import cn.wgx.common.entity.sys.Permission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Set;

public interface PermissionMapper extends IBaseMapper<Permission> {

//    @Select("SELECT  DISTINCT p.*  FROM  sys_role_user ru " +
//            "LEFT JOIN sys_role_permission rp ON ru.role_id = rp.role_id " +
//            "LEFT JOIN sys_permission p ON rp.permission_id = p.id " +
//            "WHERE ru.user_id = #{userId}")
        @Select("SELECT DISTINCT p.* FROM sys_user AS u " +
                "LEFT JOIN sys_role_user  AS ur ON u.user_id = ur.user_id " +
                "LEFT JOIN sys_role AS r ON r.role_id = ur.role_id " +
                "LEFT JOIN sys_role_permission AS rp ON r.role_id = rp.role_id " +
                "LEFT JOIN sys_permission AS p ON p.permission_id = rp.permission_id " +
                "WHERE u.user_id = #{userId}")

    Set<Permission> getPerByUserId(Long userId);

    @Select("<script>" +
                "SELECT sp.* FROM sys_permission sp " +
                "LEFT JOIN sys_role_permission rp ON sp.permission_id=rp.permission_id " +
                "WHERE rp.role_id IN " +
                "<foreach collection='roleIds' separator=',' item='id' open='(' close=')'>" +
                    "#{id}" +
                "</foreach>" +
            "</script>")
    Set<Permission> getPerByRoleIds(@Param("roleIds") Set<Long> roleIds);


}
